//Page Object
import regeneratorRuntimer, { async } from '../../lib/runtime/runtime'
import { request } from '../../request/index.js'
import { showToast, login } from "../../utils/asyncWx.js"
const md5 = require('../../utils/md5.js');
const config = require("../../utils/config.js")
const utils = require("../../utils/utils.js")
Page({
    data: {
        //授权按钮显示
        flag: true,
        showDialog: false
    },
    // openid用于微信用户登录
    openid: "",
    userkey: {
        loginmsg: "",
        credential: "",
        logintype: ""
    },
    userEnInfoVo: {
        encryptedData: "",
        iv: "",
        thirdSessionkey: ""
    },
    mobileEnInfoVo: {
        encryptedData: "",
        iv: "",
        thirdSessionkey: ""
    },
    //弹窗显示
    showModal: false,

    // 船舶id参数
    id: "",

    // 页面启动的时候
    onLoad(e) {
        console.log(e)
        console.log(e.id)
        console.log(typeof(e.id))
        if (e.id) {
            this.id = e.id
            this.checkout()
        } else {
            // 这是外扫码场景值
            const url = decodeURIComponent(e.q)
            const index = url.lastIndexOf('=')
            const number = url.slice(index + 1, url.length)
            console.log(number)
            this.id = number
                // 检测是否已经注册
            console.log(this.id)
            this.checkout()
        }





    },
    // 授权信息
    async handleGetUserInfo(e) {
        console.log(e)
        const { userInfo } = e.detail;
        this.userEnInfoVo.encryptedData = e.detail.encryptedData
        this.userEnInfoVo.iv = e.detail.iv
            // 获取到用户信息
        if (userInfo != undefined) {
            wx.setStorageSync("userInfo", userInfo);
            const res = await login()
            const { code } = res
            // 获取响应头的set-cookie的信息
            const results = await request({ url: "/wechat/getWeChatUserSessionKey.controller", data: { jsCode: code }, method: "POST" })
            wx.setStorageSync('openid', results.data.sessionKey.openid)
            this.userkey.loginmsg = results.data.sessionKey.openid
            this.userkey.credential = md5.hexMD5(results.data.sessionKey.openid)
            this.userEnInfoVo.thirdSessionkey = results.data.sessionKey.session_key
            this.mobileEnInfoVo.thirdSessionkey = results.data.sessionKey.session_key
            this.setData({
                flag: false,
                showDialog: true
            })
            this.checkout()
        } else {
            await showToast({
                title: "请授权登录"
            })
        }
    },
    // 用于检测
    async checkout() {
        this.userkey.loginmsg = wx.getStorageSync('openid')
        this.userkey.credential = md5.hexMD5(wx.getStorageSync('openid'))
        if (this.userkey.loginmsg.length > 0) {
            const res = await request({ url: "/wechat/checkUserInfo.controller", data: { userkey: JSON.stringify(this.userkey) }, method: "POST" })
            if (res.data.isLogin == false) {
                await showToast({
                    title: "请绑定手机号码"
                })
            } else {
                wx.redirectTo({ url: '/pages/user_page/index?id=' + this.id });
            }
        } else {
            await showToast({
                title: "请授权"
            })
        }
    },
    // 用于用户登录
    async login() {
        const res = await request({ url: "/wechat/weChatForSuperwRegister.controller", data: { mobileEnInfoVo: JSON.stringify(this.mobileEnInfoVo), userEnInfoVo: JSON.stringify(this.userEnInfoVo) }, method: "POST" })
        console.log(res)
        if (res.msg === '登录成功') {
            wx.redirectTo({ url: '/pages/user_page/index?id=' + this.id });
            await showToast({
                title: '登录成功'
            })
        } else {
            await showToast({
                title: "登录失败"
            })
        }
    },
    // 用于获取手机号码
    async getPhoneNumber(e) {

        if (e.detail.errMsg == 'getPhoneNumber:ok') {
            console.log(e)
            this.mobileEnInfoVo.encryptedData = e.detail.encryptedData
            this.mobileEnInfoVo.iv = e.detail.iv
            this.login()
        } else {
            await showToast({
                title: "请授权"
            })
        }
    },
});